package wifiunion.com.notice.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import wifiunion.com.notice.BaseSubFragment;
import wifiunion.com.notice.Constants;
import wifiunion.com.notice.R;
import wifiunion.com.notice.adapter.ConfigueAdapter;
import wifiunion.com.notice.entity.ConfigueTitle;
import wifiunion.com.notice.entity.MenuEntity;
import wifiunion.com.notice.entity.MenuResponse;
import wifiunion.com.notice.entity.User;
import wifiunion.com.notice.utils.SharedPreferencesUtils;

/**
 * 配置首屏
 * Created by Administrator on 2016/11/23.
 */

public class AddFirstScreenFragment extends BaseSubFragment implements View.OnClickListener {
    private View mMainView;


    private FragmentStatePagerAdapter mAdapter;

    private ViewPager mViewPager;

    private android.support.design.widget.TabLayout mTabLayout;

    private ArrayList<String> mFragmentTitleList = new ArrayList<>();


    private ImageView mCoverLayoutDownBtn;


    public static AddFirstScreenFragment newInstance() {
        AddFirstScreenFragment asf = new AddFirstScreenFragment();
        return asf;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mMainView = inflater.inflate(R.layout.fragment_addfirstscreen, container, false);
        //进入时默认切换到用自有数据
        Constants.isOwnData = true;
        String token = SharedPreferencesUtils.getSharedPreferences(Constants.CONFIG_PLACE_AUTHTOKEN, getActivity());
        SharedPreferencesUtils.setSharedPreferences(Constants.CONFIG_AUTHTOKEN, token,
                getContext());
//        mAuthtoken = SharedPreferencesUtils.getSharedPreferences(Constants.CONFIG_AUTHTOKEN, getActivity());

        User placeUser = SharedPreferencesUtils.getPlaceUserSharedPreferences(getContext());
        SharedPreferencesUtils.setUserSharedPreferences(placeUser,getContext());
//        user = placeUser;
//        mChooseFilterUuidEntity = null;
        Constants.refreshIP();
        initview();
        return mMainView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mTabLayout = (TabLayout) mMainView.findViewById(R.id.configue_tabLayout);
        mViewPager = (ViewPager) mMainView.findViewById(R.id.configue_viewpager);
        refreshTitle();
        mAdapter = new ConfigueAdapter(getContext(), getFragmentManager(), mFragmentTitleList);
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            View view = getTabView(i);
            tab.setCustomView(view);
        }
    }


    public View getTabView(int position) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.tab_text_layout, null);
        TextView tv = (TextView) view.findViewById(R.id.tab_tv);
        tv.setText(mAdapter.getPageTitle(position));
        return view;
    }


    @Override
    public void initview() {
        mCoverLayoutDownBtn = (ImageView) mMainView.findViewById(R.id.down_btn);
        mCoverLayoutDownBtn.setOnClickListener(this);
    }

    @Override
    public void initData() {


    }


    public void refreshTitle() {
        if (!mFragmentTitleList.isEmpty()) {
            return;
        }

        ArrayList<ConfigueTitle> result = new ArrayList<>();
        result.add(new ConfigueTitle(1, "cover"));
        result.add(new ConfigueTitle(2, "notice"));
        result.add(new ConfigueTitle(3, "tzbg"));
        for (ConfigueTitle ct : result) {
            mFragmentTitleList.add(ct.getName());
        }

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.down_btn) {
            getContext().sendBroadcast(new Intent(Constants.HOMEPAGE_MENUNEWNOTICE));
        }
    }


    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}
